﻿using System.Windows;
using FileSecurityManager.BizEntities;
using FileSecurityManager.BizLogic;

namespace FileSecurityManager.UI
{
    using System;

    /// <summary>
    /// Interaction logic for UserInfo.xaml
    /// </summary>
    public partial class UserInfo
    {
        private readonly UsersLogic logic = new UsersLogic();
        private readonly RoleLogic roleLogic = new RoleLogic();
        private readonly UserModel userModel;

        public UserInfo(UserModel model)
        {
            InitializeComponent();
            
            userModel = model;
            loadUserInfo();
        }
        
        private void loadUserInfo()
        {
            // binding data grid
            cbRole.ItemsSource = roleLogic.GetRoles();

            if (userModel != null && userModel.ItemId > 0)
            {
                tbFirstName.Text = userModel.FirstName;
                tbLastName.Text = userModel.LastName;
                tbMiddleName.Text = userModel.MiddleName;
                tbPassword.Password = userModel.Password;
                tbPosition.Text = userModel.Position;
                tbUserName.Text = userModel.UserName;
                cbRole.SelectedValue = userModel.UserRole.RoleId;
            }
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            int roleId = Convert.ToInt32(cbRole.SelectedValue);

            if (userModel == null)
            {
                var model = new UserModel
                {
                    ItemId = 0,
                    FirstName = tbFirstName.Text,
                    LastName = tbLastName.Text,
                    MiddleName = tbMiddleName.Text,
                    Password = tbPassword.Password,
                    Position = tbPosition.Text,
                    UserName = tbUserName.Text,
                    UserRole = new UserRoleModel()
                    {
                        ItemId = 0,
                        RoleId = roleId,
                        RoleName = cbRole.Text
                    }
                };

                logic.AddUser(model);
            }
            else
            {
                userModel.FirstName = tbFirstName.Text;
                userModel.LastName = tbLastName.Text;
                userModel.MiddleName = tbMiddleName.Text;
                userModel.Password = tbPassword.Password;
                userModel.Position = tbPosition.Text;
                userModel.UserName = tbUserName.Text;
                userModel.UserRole = new UserRoleModel
                    {
                        ItemId = userModel.ItemId,
                        RoleId = roleId, RoleName = cbRole.Text
                    };

                logic.UpdateUser(userModel);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }
    }
}
